package com.quan.system.common.mq.consumer;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.quan.common.base.constant.CommonConstant;
import com.quan.system.entity.SystemUser;
import com.rabbitmq.client.Channel;

/**
 * 消费端-消息队列监听
 * 
 * @author yhaoquan
 *
 */
@Component
public class TestMessageConsumer01 {

	private Logger logger = LoggerFactory.getLogger(getClass());

	// 配置监听器
	@RabbitListener(
			bindings = @QueueBinding(
					//队列(队列的名称,是否持久化)
					value = @Queue(value = CommonConstant.QUEUE_TOPIC_TEST, durable = "true"),
					//交换机(交换机的名称,是否持久化,交换机类型)
					exchange = @Exchange(name = CommonConstant.EXCHANGE_TOPIC_TEST, durable = "true", type = "topic"),
					//队列Routiny Key
					key = CommonConstant.QUEUE_TOPIC_TEST+".TestMessageConsumer01"
			)
	)

	@RabbitHandler
	public void process(@Payload SystemUser entity, @Headers Map<String, Object> headers, Channel channel)
			throws Exception {
		logger.info("-----------------------TestMessageConsumer01>>>收到消息，开始消费Topic队列消息----------------------------");

		// ACK签收（delivery_tag，是否批量签收）
		// 告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
		channel.basicAck((Long) headers.get(AmqpHeaders.DELIVERY_TAG), false);

	}

}
